<?php
/**
 * Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
OCP\JSON::checkAppEnabled('calendar');

if( !OC_USER::isLoggedIn() ) {
   jexit(jsonError('您需要先登录', null, false));
}

if (!PSession::checkToken('post')) {
   jexit(jsonError('验证已失效，请重新验证', null, false));
}
$object_model = new PCalendarModelObject();
$post = JRequest::get("post");

// Check CURD Permissions
if ( !$object_model->checkPermissions($post) ) {
   jexit(jsonError($object_model->getError(), null, false));
}

$reback = $object_model->validateRequest($post);
if (!$reback) {
   jexit(jsonError($object_model->getError(), null, false));
}else{
   $post = $reback;
}

$cal = JRequest::getInt('calendar', 0);
$vcalendar = $object_model->createVCalendarFromRequest($post);

if ( !$object_model->add($cal, $vcalendar->serialize(), $post) ) {
   jexit(jsonError($object_model->getError(), null, false));
}
jexit(jsonSuccess('安排成功', null, null, false));
